<?php
/**
 * Created by PhpStorm.
 * User: 汪利东
 * Date: 2016/3/24
 * Time: 14:35
 */

namespace Admin\Controller;


class BookTypeController extends BaseController
{
    public function index()
    {
        $data = M('book_type')->where(['pid' => 0])->select();
        foreach ($data as $k => $v) {
            $data[$k]['child'] = M('book_type')->where(['pid' => $v['type_id']])->select();
        }

        $this->data = $data;

        $this->display();
    }

    function getType(){
        $type_id=I('post.type_id');
        $d=M('Book_type')->where(['type_id'=>$type_id])->find();
        if($d){
            $j=[
                'status'=>'success',
                'data'=>$d
            ];
        }else{
            $j=[
                'status'=>'error',
                'data'=>'数据不存在'
            ];
        }
        echo json_encode($j,JSON_UNESCAPED_UNICODE);
    }

    function dosave(){
        $type_id=I('post.type_id');
        $type_name=I('post.type_name');
        $is_recommend=I('post.is_recommend');
        $r=M('Book_type')->where(['type_id'=>$type_id])->data(['name'=>$type_name,'is_recommend'=>$is_recommend])->save();
        if($r){
            $j=[
                'status'=>'success',
                'data'=>'保存成功'
            ];
        }else{
            $j=[
                'status'=>'success',
                'data'=>'保存成功,未做任何修改'
            ];
        }
        echo json_encode($j,JSON_UNESCAPED_UNICODE);
    }

    function doadd(){
        $up_id=I('post.up_id');
        $type_name=I('post.type_name');
        $is_recommend=I('post.is_recommend');
        $r=M('Book_type')->data(['name'=>$type_name,'pid'=>$up_id,'is_recommend'=>$is_recommend])->add();
        if($r){
            $j=[
                'status'=>'success',
                'data'=>'新增成功'
            ];
        }else{
            $j=[
                'status'=>'error',
                'data'=>'新增失败'
            ];
        }
        echo json_encode($j,JSON_UNESCAPED_UNICODE);
    }

    function dodel(){
        $type_id=I('post.type_id');
        $r=M('Book_type')->where(['type_id'=>$type_id])->delete();
        if($r){
            $j=[
                'status'=>'success',
                'data'=>'删除成功'
            ];
            M('Book_type')->where(['pid'=>$type_id])->delete();
        }else{
            $j=[
                'status'=>'error',
                'data'=>'删除失败'
            ];
        }
        echo json_encode($j,JSON_UNESCAPED_UNICODE);
    }
}